/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;


import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

// TODO: Auto-generated Javadoc
/**
 * The Class StereotypeAppearanceSection.
 * 
 * @author Gilles Cannenterre
 * @since 29 nov. 06
 */
public class StereotypeAppearanceSection extends PropertyViewSection {

	/**
	 * The stereotype appearance.
	 */
	private CLabel stereotypeAppearance;
	
	/**
	 * The stereotype text alignment.
	 */
	private CLabel stereotypeTextAlignement;
	
	/**
	 * The combo stereotype appearance.
	 */
	private CCombo comboStereotypeAppearance;
	
	/**
	 * The combo stereotype alignment.
	 */
	private CCombo comboStereotypeAlignement;	
	
	/**
	 * The combo stereotype appearance listener.
	 */
	private SelectionListener comboStereotypeAppearanceListener;	
	
	/**
	 * The combo stereotype alignment listener.
	 */
	private SelectionListener comboStereotypeAlignementListener;

	/**
	 * Creates the controls.
	 * 
	 * @param tabbedPropertySheetPage the tabbed property sheet page
	 * @param parent the parent
	 */
	@Override
	public void createControls(Composite parent,TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;
		
		comboStereotypeAppearance = getWidgetFactory().createCCombo(composite, SWT.BORDER);
		comboStereotypeAppearance.add("Text");
		comboStereotypeAppearance.add("Icon");
		comboStereotypeAppearance.add("Text and Icon");
		comboStereotypeAppearance.add("Shape");
		data = new FormData();
		data.left = new FormAttachment(0, 135);
		data.top = new FormAttachment(0, 0);
		comboStereotypeAppearance.setLayoutData(data);

		comboStereotypeAppearanceListener = new SelectionListener(){
			public void widgetSelected(SelectionEvent e) {
				if (diagramElement != null ) {
					if (((GraphElement)diagramElement).getSemanticModel() != null ){
						
						org.eclipse.uml2.uml.Element elt     = ((Uml1SemanticModelBridge) ((GraphElement)diagramElement).getSemanticModel()).getElement();
						org.eclipse.swt.graphics.Image icon =PapyrusPlugin.getIconElement(elt);
						org.eclipse.swt.graphics.Image shape =PapyrusPlugin.getShapeElement(elt);
						
						if(comboStereotypeAppearance.getText().equals("Text")){
							((GraphElement)diagramElement).getSemanticModel().setPresentation(ModelerHelper.TEXT_STEREOTYPE_PRESENTATION);
						}else if(comboStereotypeAppearance.getText().equals("Icon") && (icon != null)){
							((GraphElement)diagramElement).getSemanticModel().setPresentation(ModelerHelper.ICON_STEREOTYPE_PRESENTATION);
						}else if(comboStereotypeAppearance.getText().equals("Text and Icon") && (icon != null)){
							((GraphElement)diagramElement).getSemanticModel().setPresentation(ModelerHelper.TEXT_ICON_STEREOTYPE_PRESENTATION);
						}else if(comboStereotypeAppearance.getText().equals("Shape") && (shape != null)){
							((GraphElement)diagramElement).getSemanticModel().setPresentation(ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION);
						}
						refresh();
					}
				}				
			}

			public void widgetDefaultSelected(SelectionEvent e) {}
		};
		comboStereotypeAppearance.addSelectionListener(comboStereotypeAppearanceListener);
		
		stereotypeTextAlignement = getWidgetFactory().createCLabel(composite, "Text Alignement:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(comboStereotypeAppearance, 40);
		data.top = new FormAttachment(comboStereotypeAppearance, 1, SWT.CENTER);
		stereotypeTextAlignement.setLayoutData(data);

		comboStereotypeAlignement = getWidgetFactory().createCCombo(composite, SWT.BORDER);
		comboStereotypeAlignement.add("Horizontal");
		comboStereotypeAlignement.add("Vertical");
		data = new FormData();
		data.left = new FormAttachment(stereotypeTextAlignement, 0);
		data.top = new FormAttachment(0, 0);
		comboStereotypeAlignement.setLayoutData(data);
	
		comboStereotypeAlignementListener = new SelectionListener () {
			public void widgetSelected(SelectionEvent e) {
				if (((GraphElement)diagramElement) != null) {
					if (((GraphElement)diagramElement).getSemanticModel() != null) {
						
						if(comboStereotypeAlignement.getText().equals("Horizontal")) {
							((GraphElement)diagramElement).getSemanticModel().setPresentation(ModelerHelper.STEREOTYPE_TEXT_HORIZONTAL_PRESENTATION);
						}else if(comboStereotypeAlignement.getText().equals("Vertical")) {
							((GraphElement)diagramElement).getSemanticModel().setPresentation(ModelerHelper.STEREOTYPE_TEXT_VERTICAL_PRESENTATION);
						}
						refresh();
					}
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {}
		};
		comboStereotypeAlignement.addSelectionListener(comboStereotypeAlignementListener);
		
		stereotypeAppearance = getWidgetFactory().createCLabel(composite, "Stereotype Appearance:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(comboStereotypeAppearance,0);
		data.top = new FormAttachment(comboStereotypeAppearance, 1, SWT.CENTER);
		stereotypeAppearance.setLayoutData(data);
	}


	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * Refresh.
	 */
	@Override
	public void refresh() {
		if ((!comboStereotypeAppearance.isDisposed()) && (!comboStereotypeAlignement.isDisposed())){
			
			comboStereotypeAppearance.removeSelectionListener(comboStereotypeAppearanceListener);
			comboStereotypeAlignement.removeSelectionListener(comboStereotypeAlignementListener);
			
			if (diagramElement != null ){
				if (((GraphElement)diagramElement).getSemanticModel() != null ){
					comboStereotypeAppearance.setEnabled(true);	
					comboStereotypeAlignement.setEnabled(true);
					
					final String stereotypePresentation = ((GraphElement)diagramElement).getSemanticModel().getPresentation();
					
					if (stereotypePresentation != null ){
						
						org.eclipse.uml2.uml.Element elt     = ((Uml1SemanticModelBridge) ((GraphElement)diagramElement).getSemanticModel()).getElement();
						org.eclipse.swt.graphics.Image icon =PapyrusPlugin.getIconElement(elt);
						org.eclipse.swt.graphics.Image shape = PapyrusPlugin.getShapeElement(elt);

						if(stereotypePresentation.equals(ModelerHelper.TEXT_STEREOTYPE_PRESENTATION)) {
							comboStereotypeAlignement.setEnabled(true);
							comboStereotypeAppearance.setText("Text");
						} else if(stereotypePresentation.equals(ModelerHelper.ICON_STEREOTYPE_PRESENTATION) && (icon != null)) {
							comboStereotypeAlignement.setEnabled(false);
							comboStereotypeAppearance.setText("Icon");
						} else if(stereotypePresentation.equals(ModelerHelper.TEXT_ICON_STEREOTYPE_PRESENTATION) && (icon != null)) {
							comboStereotypeAlignement.setEnabled(true);
							comboStereotypeAppearance.setText("Text and Icon");
						} else if(stereotypePresentation.equals(ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION) && (shape != null)) {
							comboStereotypeAlignement.setEnabled(false);
							comboStereotypeAppearance.setText("Shape");
						} else {
							comboStereotypeAlignement.setEnabled(true);
							comboStereotypeAppearance.setText("Text");  	
						}
						
						if(stereotypePresentation.equals(ModelerHelper.STEREOTYPE_TEXT_HORIZONTAL_PRESENTATION)) {
							comboStereotypeAlignement.setText("Horizontal");
						} 
						else if(stereotypePresentation.equals(ModelerHelper.STEREOTYPE_TEXT_VERTICAL_PRESENTATION)) {
							comboStereotypeAlignement.setText("Vertical");
						} 
						else {
							comboStereotypeAlignement.setText("Horizontal");  	
						}
						
						
					}else{
						comboStereotypeAppearance.setText("Text");
						comboStereotypeAlignement.setText("Horizontal");
					}
					
				} else {
					comboStereotypeAppearance.setText("");
					comboStereotypeAppearance.setEnabled(false);
					comboStereotypeAlignement.setText("");
					comboStereotypeAlignement.setEnabled(false);
				}
			}
			
			comboStereotypeAppearance.addSelectionListener(comboStereotypeAppearanceListener);
			comboStereotypeAlignement.addSelectionListener(comboStereotypeAlignementListener);
			
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(comboStereotypeAppearance!=null && !comboStereotypeAppearance.isDisposed())
			comboStereotypeAppearance.removeSelectionListener(comboStereotypeAppearanceListener);
		if(comboStereotypeAlignement!=null && !comboStereotypeAlignement.isDisposed())
			comboStereotypeAlignement.removeSelectionListener(comboStereotypeAlignementListener);
	}
}

